package com.xzmzhp.service.power.impl;

import cn.hutool.core.collection.CollUtil;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.dao.power.RequestPathMapper;
import com.xzmzhp.pojo.power.Competence;
import com.xzmzhp.pojo.power.RequestPath;
import com.xzmzhp.service.power.RequestPathService;
import com.xzmzhp.utils.return_tool.ReturnData;
import com.xzmzhp.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王源(xm)
 * @Date: 2020/12/9 09:16
 * @Description: 请求路径接口实现类
 */
@Service
public class RequestPathServiceImpl implements RequestPathService {

    /**
     * 注入请求路径操作类
     */
    @Autowired
    private RequestPathMapper requestPathMapper;

    /**
     * 查询所有请求路径
     *
     * @return ReturnData
     */
    @Override
    public List<RequestPath> findAll() {
        return requestPathMapper.findAll();
    }

    /**
     * 根据请求路径模糊查询
     *
     * @param url 请求路径
     * @return ReturnData
     */
    @Override
    public ReturnData findByUrl(String url) {
        if (StringUtils.isNotEmpty(url)) {
            List<RequestPath> list = requestPathMapper.findByUrl(url);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL, "请求路径为空");
    }

    /**
     * 根据请求路径查询所对应的权限
     *
     * @param url 请求路径
     * @return ReturnData
     */
    @Override
    public List<Competence> findByOneUrl(String url) {
        if (StringUtils.isNotEmpty(url)) {
            //根据请求路径查询请求id
            Integer id = requestPathMapper.findByUrl(url).get(0).getId();
            //根据请求路径id查询权限
            List<Competence> competenceList = requestPathMapper.findByOneUrl(id);
            if (StringUtils.isNotNull(competenceList)) {
                return competenceList;
            }
        }
        return null;
    }

    /**
     * 根据请求路id查询
     *
     * @param id 请求路径id
     * @return 请求路径集合
     */
    @Override
    public RequestPath findById(Integer id) {
        if (id != null && id > 0) {
            return requestPathMapper.findById(id);
        }
        return null;
    }

    /**
     * 批量添加请求路径
     * @param paths 请求路径
     * @return 是否成功
     */
    @Override
    public Integer addRequestPaths(List<String> paths) {
        //定义存储请求路径对象集合
        List<RequestPath> list = new ArrayList<>();
        //创建对象引用
        RequestPath requestPath = null;
        //获取所有路径
        List<String> requestPaths =  requestPathMapper.findAllRequestPath();
        //将所有请求路径转化为请求路径对象并添加进list集合
        for (String path : paths) {
            //判断路径是否存在
            if (!requestPaths.contains(path)) {
                continue;
            }
            requestPath = new RequestPath();
            requestPath.setUrl(path);
            list.add(requestPath);
        }
        //若不为空则添加
        if (CollUtil.isNotEmpty(list)) {
            return requestPathMapper.addRequestPaths(list);
        }
        return 0;
    }

    /**
     * 修改请求路径
     *
     * @param requestPath 请求路径集合
     * @return 是否成功
     */
    @Override
    public Integer updateRequestPath(RequestPath requestPath) {
        //判断对象是否为空
        if (requestPath != null) {
            return requestPathMapper.updateRequestPath(requestPath);
        }
        return 0;
    }

    /**
     * 删除请求路径
     *
     * @param id 请求路径id
     * @return 是否成功
     */
    @Override
    public Integer deleteRequestPath(Integer id) {
        //判断对象是否为空
        if (id != null && id > 0) {
            return requestPathMapper.deleteRequestPath(id);
        }
        return 0;
    }
}
